package com.im;

import mapgenerator.*;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.netbeans.lib.awtextra.AbsoluteConstraints;
import org.netbeans.lib.awtextra.AbsoluteLayout;

/**
 * ImageManagerToImagePatternDialog.java
 *
 * Created on Marc 15, 2009, 8:13:01 PM
 * 
 * @author Sajarwo Anggai
 */
public class IMToImagePatternDialog extends JDialog implements ActionListener, KeyListener, ChangeListener {

    private JButton Cancel;
    private JButton OK;
    private JLabel jLabelBackground;
    private JLabel jLabelColumns;
    private JLabel jLabelName;
    private JLabel jLabelRows;
    private JLabel jLabelSlice;
    private JSpinner jSpinnerColumns;
    private JSpinner jSpinnerRows;
    private JTextField jTextFieldName;
    private Main comp;
    private String currenImage;
    private String name = "";
    private int cols;
    private int rows;
    private boolean asSprite;

    public IMToImagePatternDialog(Main comp, String currentImage,boolean asSprite) {
        this.comp = comp;
        this.currenImage = currentImage;
        this.asSprite = asSprite;
        initComponents();
    }

    private void initComponents() {

        jLabelSlice = new JLabel();
        jLabelName = new JLabel();
        jLabelColumns = new JLabel();
        jLabelRows = new JLabel();
        jTextFieldName = new JTextField();
        jSpinnerColumns = new JSpinner();
        jSpinnerRows = new JSpinner();
        OK = new JButton();
        Cancel = new JButton();
        jLabelBackground = new JLabel();


        setTitle("Slice to Imagege Pattern");
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new Dimension(415, 240));
        setLocationByPlatform(true);
        setResizable(false);

        getContentPane().setLayout(new AbsoluteLayout());

        jLabelSlice.setText("Slice Image to Image Pattern");
        getContentPane().add(jLabelSlice, new AbsoluteConstraints(20, 20, -1, -1));

        jLabelName.setText("Name of Image Pattern:");
        getContentPane().add(jLabelName, new AbsoluteConstraints(20, 60, -1, -1));

        jLabelColumns.setText("Number of Columns:");
        getContentPane().add(jLabelColumns, new AbsoluteConstraints(20, 90, -1, -1));

        jLabelRows.setText("Number of Rows:");
        getContentPane().add(jLabelRows, new AbsoluteConstraints(20, 120, -1, -1));

        jTextFieldName.addKeyListener(this);
        jSpinnerColumns.addKeyListener(this);
        jSpinnerRows.addKeyListener(this);
        jSpinnerColumns.addChangeListener(this);
        jSpinnerRows.addChangeListener(this);

        getContentPane().add(jTextFieldName, new AbsoluteConstraints(160, 60, 230, -1));
        getContentPane().add(jSpinnerColumns, new AbsoluteConstraints(160, 90, 70, -1));
        getContentPane().add(jSpinnerRows, new AbsoluteConstraints(160, 120, 70, -1));

        OK.setText("OK");
        OK.setEnabled(false);
        OK.addActionListener(this);
        getContentPane().add(OK, new AbsoluteConstraints(240, 160, 70, -1));

        Cancel.setText("Cancel");
        Cancel.addActionListener(this);
        getContentPane().add(Cancel, new AbsoluteConstraints(320, 160, 70, -1));

        jLabelBackground.setIcon(new ImageIcon(getClass().getResource("/com/images/ProjectDialogBackground.png"))); // NOI18N
        getContentPane().add(jLabelBackground, new AbsoluteConstraints(0, 0, 415, 240));

        pack();
    }

    private void validateButtonOK() {
        try {
            cols = Integer.parseInt("" + jSpinnerColumns.getValue());
            rows = Integer.parseInt("" + jSpinnerRows.getValue());
            name = jTextFieldName.getText().trim();
            if (cols < 1 || rows < 1 || name.equals("")) {
                OK.setEnabled(false);
            } else {
                OK.setEnabled(true);
            }
        } catch (NumberFormatException e) {
            OK.setEnabled(false);
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == OK) {
            comp.ipContainer.addImage(currenImage, rows, cols, asSprite);
            comp.ipPanelWidget.reloadAll();
        }
        this.dispose();
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {
        validateButtonOK();
    }

    public void stateChanged(ChangeEvent e) {
        validateButtonOK();
    }
}
